<template>
  <div class>
    <el-card class="form">
      <el-form inline :model="searchForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="searchForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="searchForm.usertype">
            <el-option label="所有用户" value></el-option>
            <el-option label="中心用户" value="1"></el-option>
            <el-option label="管理部用户" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" type="primary" @click="init">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="selection" fixed="left" align="center"></el-table-column>
      <el-table-column prop="username" fixed="left" align="center" label="用户名"></el-table-column>
      <el-table-column prop="realName" fixed="left" align="center" label="真实姓名"></el-table-column>
      <el-table-column :formatter="userTypeFilter" prop="usertype" align="center" label="用户类型"></el-table-column>
      <el-table-column prop="mobile" align="center" label="手机号码"></el-table-column>
      <el-table-column prop="telephone" align="center" label="电话号码"></el-table-column>
      <el-table-column prop="email" align="center" label="电子邮箱"></el-table-column>
      <el-table-column width="100px" label="操作" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="addUser(scope.$index, scope.row)">添加</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-card style="text-align:right;">
      <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]" :page-size="pageSize" background layout="total, sizes, prev, pager, next"
        :total="total"></el-pagination>
    </el-card>
  </div>
</template>

<script>
  import { filterUsertype } from "@/filters/usersFilter"
  import { getList } from "../../yhgl/function";
  import { addRoleUser } from "../function";
  export default {
    components: {},
    props: {
      roleId: [String]
    },
    data() {
      return {
        formLabelWidth: "80px",
        tableData: [],
        searchForm: {
          username: "",
          usertype: ""
        },
        total: 0,
        pageSize: 10,
        currentPage: 1
      };
    },
    computed: {},
    mounted() {
      this.init();
    },
    methods: {
      userTypeFilter(a, b, c) {
        return filterUsertype(c)
      },
      sizeChange(n) {
        this.pageSize = n;
        this.init();
      },
      currentChange(n) {
        this.currentPage = n;
        this.init();
      },
      addUser(i, row) {
        let params = {
          roleId: this.roleId,
          userIds: row.id
        };
        addRoleUser(params).then(res => {
          if (res.retHead.code == "0000000") {
            this.$message.success(res.retHead.message);
            this.$emit("success")
          } else {
            this.$message.error(res.retHead.message);
          }
        });
      },
      init() {
        let params = {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          username: this.searchForm.username,
          usertype: this.searchForm.usertype
        };
        getList(params).then(res => {
          if (res.retHead.code == "0000000") {
            this.tableData = res.retBody.list;
            this.total = res.retBody.total;
            this.currentPage = res.retBody.pageNum;
          }
        });
      }
    },
    created() { }
  };
</script>
<style lang='scss' scoped>
  .btns {
    text-align: right;
  }
</style>